package com.jadefashion.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jadefashion.health.entity.QueryPageBean;
import com.jadefashion.health.resources.RedisConstant;
import com.jadefashion.mapper.CheckGroupMapper;
import com.jadefashion.mapper.CheckitemMapper;
import com.jadefashion.mapper.SetmealServiceMapper;
import com.jadefashion.pojo.CheckGroup;
import com.jadefashion.pojo.CheckItem;
import com.jadefashion.pojo.Setmeal;
import com.jadefashion.service.SetmealService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(timeout = 3000)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealServiceMapper setmealServiceMapper;
    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckitemMapper checkitemMapper;

    @Override
    public Setmeal findSetMealDetail(Integer id) {
        Setmeal setmeal=setmealServiceMapper.findSetMealById(id);
        setmeal.setId(id);

        List<CheckGroup> checkGroupList=checkGroupMapper.findCheckGroupBySetmealId(id);
        if(checkGroupList!=null&&checkGroupList.size()>0){
            for (CheckGroup checkGroup : checkGroupList) {
                List<CheckItem> checkItemList=checkitemMapper.findCheckItemByGroupId(checkGroup.getId());
                checkGroup.setCheckItems(checkItemList);
            }
        }
        setmeal.setCheckGroups(checkGroupList);
        return setmeal;
    }

    @Override
    public Setmeal findPreSetmealByid(Integer id) {
        return setmealServiceMapper.findPreSetmealByid(id);
    }

    @Override
    public List<Setmeal> findAll() {
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return setmealServiceMapper.findAll();
    }

    @Override
    public void deleteMealById(Integer id) {
        //先解除关系多对多
        setmealServiceMapper.deleteGroupIdByMealId(id);
        setmealServiceMapper.deleteMealById(id);
    }

    @Override
    public void updateSetmeal(Integer[] checkgroupIds, Setmeal setmeal) {

        //更改检查组数据
        setmealServiceMapper.update(setmeal);
        //先解除关系多对多
        setmealServiceMapper.deleteGroupIdByMealId(setmeal.getId());
        //再绑定关系多对多
        //for (Integer id : checkgroupIds) {
        //    setmealServiceMapper.addMealIdAndGroupId(setmeal.getId(),id);
        //}
		setmealServiceMapper.addMealIdAndGroupId(setmeal.getId(), Arrays.asList(checkgroupIds));
    }

    @Override
    public Integer[] findCheckItemIdsByGroupId(Integer id) {
        return setmealServiceMapper.findCheckItemIdsByGroupId(id);
    }

    @Override
    public Map<String, Object> findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<Setmeal> list= setmealServiceMapper.findPage(queryPageBean.getQueryString());
        PageInfo<Setmeal> setmealPageInfo = new PageInfo<>(list);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",setmealPageInfo.getTotal());
        map.put("list",setmealPageInfo.getList());
        return map;
    }

    @Override
    public void add(Integer[] checkgroupIds, Setmeal setmeal) {
        setmealServiceMapper.addMeal(setmeal);
        Jedis resource = jedisPool.getResource();
        resource.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        resource.close();
//        for (Integer id : checkgroupIds) {
//                setmealServiceMapper.addMealIdAndGroupId(setmeal.getId(),id);
//        }
        setmealServiceMapper.addMealIdAndGroupId(setmeal.getId(), Arrays.asList(checkgroupIds));

    }
}
